
var programDir = '/rdb';

// model and store
Ext.define('RDB.model.ComboBoxModel', {
	extend: 'Ext.data.Model',
	fields: [
		{name: 'id', type: 'int'},
		{name: 'descr', type: 'string'}
	],
})

Ext.define('RDB.store.Unit', {
	extend: 'Ext.data.Store',
	model: 'RDB.model.ComboBoxModel',
	proxy: {
		type: 'ajax',
		url: programDir + '/bin/json_data_controler.py?table=unit',
		reader: {
			type: 'json',
		},
	},
});

Ext.define('RDB.store.Model', {
	extend: 'Ext.data.Store',
	model: 'RDB.model.ComboBoxModel',
	proxy: {
		type: 'ajax',
		url: programDir + '/bin/json_data_controler.py?table=model',
		reader: {
			type: 'json',
		},
	},
})

Ext.define('RDB.store.ValueType', {
	extend: 'Ext.data.Store',
	model: 'RDB.model.ComboBoxModel',
	data: [
		{id: 0, descr: 'Float'},
		{id: 1, descr: 'Integer'},
	],
});

Ext.define('RDB.store.PlanningType', {
	extend: 'Ext.data.Store',
	model: 'RDB.model.ComboBoxModel',
	data: [
		{id: 0, descr: 'Manual'},
		{id: 1, descr: 'System'},
	],
});

// end model and store

// components
Ext.define('RDB.component.ButtonMenu',{
	extend: 'Ext.panel.Panel',
	border: 0,
	padding: 3,
	bodyStyle: 'background: #dfe9f6',
	layout: {
		type: 'table',
		columns: 2,
	},
	defaults: {
		xtype: 'button',
		margin: 1,
		width: 188,
	},
});

Ext.define('RDB.component.ComboBoxUnit', {
	extend: 'Ext.form.field.ComboBox',
	alias: 'widget.rdbunit',
	store: Ext.create('RDB.store.Unit', {}),
	queryMode: 'remote',
	displayField: 'descr',
	valueField: 'id',
	minChars: 1,
	autoSelect: true,
});

Ext.define('RDB.component.ComboBoxModel', {
	extend: 'Ext.form.field.ComboBox',
	alias: 'widget.rdbmodel',
	store: Ext.create('RDB.store.Model', {}),
	queryMode: 'remote',
	displayField: 'descr',
	valueField: 'id',
	minChars: 1,
	multiSelect: true,
	editable: false,
//	listeners:{
//		added: function (component, container, pos, options){
//			console.log("component loaded");
//			this.multiValue = [];
//		},
//		change: function (field, newValue, oldValue, options){
//			console.log("ch", field, newValue, oldValue, options);
//			console.log("mv", this, this.multiValue);	
//			console.log("to", typeof(newValue));
//			if (typeof(newValue) == typeof([])) {
//				if (((this.multiValue.length > 0)&&(this.multiValue[0] != newValue[0]))||(this.multiValue.length ==	 0)){
//					this.multiValue[this.multiValue.length] = newValue[0];
//					this.setValue(this.multiValue);
//				}
//			} else {
//				if (!newValue) {
//					this.multiValue = [];
//				}
//			}
//			return this
//		},
//	}
});

Ext.define('RDB.component.ComboBoxValueType', {
	extend: 'Ext.form.field.ComboBox',
	alias: 'widget.rdbvaluetype',
	store: Ext.create('RDB.store.ValueType', {}),
	queryMode: 'local',
	displayField: 'descr',
	valueField: 'id',
	autoSelect: true,
	editable: false,
	value: 0,
});

Ext.define('RDB.component.ComboBoxPlanningType', {
	extend: 'Ext.form.field.ComboBox',
	alias: 'widget.rdbplanningtype',
	store: new Ext.create('RDB.store.PlanningType', {}),
	queryMode: 'local',
	displayField: 'descr',
	valueField: 'id',
	autoSelect: true,
	editable: false,
	value: 0,
});

// end components 


// VTypes ---------
Ext.apply(Ext.form.field.VTypes, {
	rdbCode: function(v){
		return /^\d{10}$/.test(v);
	},
	rdbCodeText: 'Not a valid Code. Example: 1111020050',
	rdbCodeMask: /\d/,
});

Ext.apply(Ext.form.field.VTypes, {
	rdbTextEng255: function(v){
		return /^[a-z \.,0-9]{0,255}$/i.test(v);
	},
	rdbTextEng255Text: 'Not a valid Description. Example: ROLLER SINGLE DOOR D130860-00',
	rdbTextEng255Mask: /[a-z \.,0-9]/i,
});

Ext.apply(Ext.form.field.VTypes, {
	rdbText255: function(v){
		return /^.{0,255}$/i.test(v);
	},
	rdbText255Text: 'Not a valid Description. Example: ШПЛИНТ ПРУЖИННЫЙ, ЧЕРТЕЖ №000145-00',
	rdbText255Mask: /./i,
});

Ext.apply(Ext.form.field.VTypes, {
	rdbInteger: function(v){
		return /^\d+$/.test(v);
	},
	rdbIntegerText: 'Not a valid value. Example: 50',
	rdbIntegerMask: /\d/,
});
// end VTypes


Ext.define('RDB.window.MaintainSpareParts', {
	extend: 'Ext.window.Window',
	title: 'Maintain spare parts',
	layout: 'fit',
	border: 0,
	width: 500,
	height: 255,
	buttons:[{
		xtype: 'button',
		text: 'Add spare parts',
	},],
	tbar:[{
		xtype: 'button',
		iconCls: '',
	}, '|'],	
	items:[{
		xtype: 'form',
		padding: 5,
		layout: {
			type: 'anchor',
		},
		border: 0,
		bodyStyle: 'background: #dfe9f6',
		defaults: {
			anchor: '100%',
			xtype: 'textfield',
			allowBlank: false,
			labelWidth: 105,
		},
		items: [{
			fieldLabel: 'Description',
			name: 'description',
			vtype: 'rdbTextEng255',
		},{
			fieldLabel: 'Descr. RUS',
			name: 'description_rus',
			vtype: 'rdbText255',
		},{
			xtype: 'fieldcontainer',
			margin: 0,
			layout: {
				type: 'hbox',
			},
			defaults:{
				xtype: 'textfield',
				allowBlank: false,
				labelWidth: 105,
				flex: 1,
			},
			items: [{
				fieldLabel: 'Code',
				name: 'code',
				vtype: 'rdbCode',
			},{
				xtype: 'rdbunit',
				fieldLabel: '&nbsp&nbspUnit',
				name: 'unit',
			},],
		},{
			xtype: 'fieldcontainer',
			margin: 0,
			layout: {
				type: 'hbox',
			},
			defaults:{
				xtype: 'textfield',
				allowBlank: false,
				labelWidth: 105,
				flex: 1,
			},
			items: [{
				fieldLabel: 'Safety stock',
				name: 'safety_stock',
				vtype: 'rdbInteger',
			},{
				xtype: 'rdbvaluetype',
				fieldLabel: '&nbsp&nbspType value',
				name: 'type_value',
			},],
		},{
			xtype: 'fieldcontainer',
			margin: 0,
			layout: {
				type: 'hbox',
			},
			defaults:{
				xtype: 'textfield',
				allowBlank: false,
				labelWidth: 105,
				flex: 1,
			},
			items: [{
				fieldLabel: 'Planning condition',
				name: 'planning_condition',
				vtype: 'rdbInteger',
			},{
				xtype: 'rdbplanningtype',
				fieldLabel: '&nbsp&nbspPlanning type',
				name: 'planning_type',
			},],
		},{
			xtype: 'rdbmodel',
			fieldLabel: 'Model',
			name: 'model',
		},],
	}],
});


Ext.define('RDB.window.MainMenu', {
	extend: 'Ext.window.Window',
	title: 'Refurbishment Database v2',
    x: 5,
    y: 5,
    height: 162,
    width: 400,
    layout: 'fit',
    closable: false,
    border: 0,
    items: [{
    	xtype: 'tabpanel',
    	defaults: {
    		layout: 'fit',
    		bodyStyle: 'background: #dfe9f6',
    	},
    	items: [{
    		title: 'Data managment',
    		items:[
    			Ext.create('RDB.component.ButtonMenu', {
    				items:[{
    					text: '<b>Maintain spare parts</b>',
    					handler: function () {
    						showDialog('mspformdialog');
    						Ext.create('RDB.window.MaintainSpareParts', {}).show();
    					}
    				},{
    					text: 'Spare parts',
    					handler: function () {showDialog('mspdialog');}
    				},{
    					text: 'Mantain repair type',
    					handler: function () {showDialog('mrtform');}
    				},{
    					text: 'Mantain customer',
    					handler: function () {showDialog('customer');}
    				},{
    					text: 'Mantain pricing',
    					handler: function () {showDialog('pricingform');}
    				},{
    					text: 'Mantain model',
    					handler: function () {showDialog('modelform');}
    				},{
    					text: 'Mantain destination',
    					handler: function () {showDialog('destinationform');}
    				},{
    					text: 'Display data',
    					handler: function () {showDialog('displayform');}
    				}],
    			}),
    		],
    	},{
    		title: 'Stock managment',
    		items:[
    			Ext.create('RDB.component.ButtonMenu', {
    				items:[{
    					text: 'Inventory adjustment',
    					handler: function () {showDialog('inventoryadjustment');}
    				},{
    					text: 'Maintain transaction type',
    					handler: function () {showDialog('transactiontype');}
    				},{
    					text: 'Inquiries and reports',
    					handler: function () {showDialog('iareports');}
    				},{
    					text: 'Cooler inventory',
    					handler: function () {showDialog('coolerinventory');}
    				},{
    					text: 'Print spare parts for order',
    					handler: function () {showDialog('printsparepartsorder');}
    				}, {
    					text: 'Maintain spare parts order',
    					handler: function () {showDialog('maintainsparepartsorder');}
    				}],
    			}),
    		],
    	},{
    		title: 'Repair note',
    		items:[
    			Ext.create('RDB.component.ButtonMenu', {
    				items:[{
    					text: 'Maintain repare note',
    					handler: function () {showDialog('repairnote');}
    				},{
    					text: 'Modify repair note',
    					handler: function () {showDialog('repairnotetable');}
    				},{
    					text: 'Report',
    					handler: function () {showDialog('report');}
    				}],
    			}),
    		],
    	},{
    		title: 'Invoicing',
    		items:[
    			Ext.create('RDB.component.ButtonMenu', {
    				items:[{
    					text: 'Print detailed invoice by period',
    					handler: function () {showDialog('PDIbP');}
    				},{
    					text: 'Print invoice by model',
    					handler: function () {showDialog('PIbM');}
    				},{
    					text: 'Print spare per repair note',
    					handler: function () {showDialog('POSPpRN');}
    				},{
    					text: 'Print repair note number',
    					handler: function () {showDialog('PRNN');}
    				}],
    			}),
    		],
    	}],
    }],
})

Ext.application({
	name: 'RDB',
	launch: function (){
		new Ext.create('RDB.window.MainMenu', {}).show();
	}
});